package com.itheima.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface MenuMapper extends BaseMapper<Menu> {
    @Select("select distinct tm.*\n" +
            "from t_menu tm,t_role_menu trm where tm.id=trm.menu_id and trm.role_id in (\n" +
            "\tselect tru.role_id \n" +
            "from t_user tu,t_role_user tru\n" +
            "where tu.id=tru.user_id and tu.user_name=#{username}\n" +
            ")")
    List<Menu> getMenus(String username);

    @Select("select tr.id \n" +
            "from t_role_menu trm,t_role tr where tr.id=trm.role_id and trm.menu_id=#{id}")
    List<Long> getRoleByMenuId(long id);

    @Select("select * from t_role")
    List<Role> getRole();

    @Delete("delete from t_role_menu where menu_id=#{menuId}")
    void deleteByMenuId(Long menuId);

    @Insert("insert into t_role_menu values(null,#{roleId},#{menuId})")
    void addMenuAndRole(@Param("menuId") Long menuId, @Param("roleId") Long roleId);
}
